<template>
  <view class="login">
    <view class="content">
      <!-- logo -->
      <view class="header">
        <image :src="logoImage" mode="aspectFit"></image>
      </view>
      
      <!-- 主体表单 -->
      <view class="main">
        <wInput
          v-model="account"
          type="text"
          placeholder="邮箱/用户名"
        />
        
        <wInput
          v-model="passData"
          type="password"
          maxlength="20"
          placeholder="密码"
          isShowPass
        />
      </view>

      <!-- 登录按钮 -->
      <wButton
        text="登 录"
        :rotate="loading"
        @click.native="startLogin"
      />
      
      <!-- 注册/忘记密码 -->
      <view class="footer">
        <text @click="goRegister">注册账号</text>
        <text>|</text>
        <text @click="goForget">忘记密码</text>
      </view>
    </view>
  </view>
</template>

<script>
import wInput from '../../components/watch-login/watch-input.vue';
import wButton from '../../components/watch-login/watch-button.vue';
import { login02 } from '../../common/api.js';

export default {
  data() {
    return {
      logoImage: '',
      account: '',
      passData: '',
      loading: false,
      errorMessage: ''
    };
  },

  components: {
    wInput,
    wButton
  },

  methods: {
    // 表单验证
    validateForm() {
      if (!this.account) {
        uni.showToast({
          title: '请输入邮箱或用户名',
          icon: 'none',
          duration: 2000
        })
        return false
      }
      if (!this.passData) {
        uni.showToast({
          title: '请输入密码',
          icon: 'none',
          duration: 2000
        })
        return false
      }
      return true
    },

    // 登录方法
    async startLogin() {
      if (!this.validateForm()) return
      
      this.loading = true
      try {
        const res = await login02(this.account, this.passData)
        console.log('登录响应:', res.data)  // 调试用

        if (res.statusCode === 200 && res.data.code === 200) {
          // 保存token和用户信息
          const userData = res.data.data
          uni.setStorageSync('token', userData.token)
          uni.setStorageSync('userInfo', {
            userId: userData.userId,
            username: userData.username,
            email: userData.email
          })
          
          // 登录成功提示
          uni.showToast({
            title: '登录成功',
            icon: 'success',
            duration: 1500
          })

          // 延迟跳转
          setTimeout(() => {
            uni.navigateTo({
              url: '/pages/index/index'
            })
          }, 0)
        } else {
          // 显示错误信息
          uni.showToast({
            title: res.data.message || '登录失败',
            icon: 'none',
            duration: 2000
          })
        }
      } catch (error) {
        console.error('登录错误:', error)
        uni.showToast({
          title: '网络错误，请稍后重试',
          icon: 'none',
          duration: 2000
        })
      } finally {
        this.loading = false
      }
    },

    // 跳转注册
    goRegister() {
      uni.navigateTo({
        url: '/pages/login/register'
      })
    },

    // 跳转忘记密码
    goForget() {
      uni.navigateTo({
        url: '/pages/login/forget'
      })
    }
  }
};
</script>



<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
